<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class manager extends MY_Controller {
    
    public function index(){
        //echo date("Y-m-d g:i a");
        $data['topics']=$this->model_topic->get_all();
        
        $this->load_view('manager/index',$data);
        //var_dump(json_encode($this->model_word->get(1)));
        
        // create 2 thread objects
        //$this->load->library('Thread'); 
        //$t1 = $this->Thread->start('onSaveImage');
         
    }
    
    public function delete_topic($id){
        if($this->model_topic->delete($id))
            echo '1';
        else
            echo '0';
    } 
    
    public function delete_word($id){
        if($this->model_word->delete($id))
            echo '1';
        else
            echo '0';
    }
    
    public function insert_topic(){
        $id=$_POST['id'];
        
        
        //down image
        //$lst=explode('/',$_POST['image']);
        //var_dump($lst);
        //echo $_POST['image'];
        //$image=$lst[count($lst)-1];
        //$image=rand(100,9999).$image;
        //echo $image;
        
        //resize image
        
        //$this->load->library('simpleimage'); 
        
        //$this->simpleimage->load($_POST['image']);
        //$this->simpleimage->resize($this->config->item('topic_width'),$this->config->item('topic_height'));
        //$this->simpleimage->save($this->config->item('asb_data').$image);
        //end resize
        
        
        $data=array(
                'name'=>$_POST['name'],
                'description'=>$_POST['description'],
                'date'=>date("Y-m-d G:i:00"),
                'image'=>$_POST['image'],
            );
        
        if($id>0){
            $data['id']=$id;
            echo $this->model_topic->update($id,$data);
        }
        else{
            echo $this->model_topic->insert($data);
        }
        
        
        
    }
    
    private function onSaveImage(){
        sleep(120);
        echo 'test';
    }
    
    
    public function insert_word(){
        //$this->load->library('SimpleImage');
        $id=$_POST['id'];
        
        //down image
        //$lst=explode('/',$_POST['image']);
        //var_dump($lst);
        //echo $_POST['image'];
        //$image=$lst[count($lst)-1];
        //$image=rand(100,9999).$image;
        //echo $image;
        
        //resize image
        
        //$config['image_library'] = 'gd';
        //$config['new_image'] =$_POST['image'];
        //$config['dest_image'] = $this->config->item('asb_data').$image;
        //$config['create_thumb'] = 'TRUE';
        //$config['width'] = '75';
        //$config['height'] = '50';
    
        //$this->image_lib->initialize($config);
        //$this ->image_lib ->initialize($img_config);
        
        //$this->load->library('image_lib',$config);
        
        //if ( ! $this->image_lib->resize())
        //{
        //    echo $this->image_lib->display_errors();
        //}
        //end resize
        
        $data=array(
                'topic_id'=>$_POST['topic_id'],
                'key'=>$_POST['name'],
                'value'=>$_POST['description'],
                'date'=>date("Y-m-d G:i:00"),
                'image'=>$_POST['image']
            );
        if($id>0){
            $data['id']=$id;
            echo $this->model_word->update($id,$data);
        }
        else{
            echo $this->model_word->insert($data);
        }
    }
    
    public function get_word($int){
        echo json_encode($this->model_word->get($int));
    }
    
    private function save_image($inPath,$outPath)
    { //Download images from remote server
        $in=    fopen($inPath, "rb");
        $out=   fopen($outPath, "wb");
        while ($chunk = fread($in,8192))
        {
            fwrite($out, $chunk, 8192);
        }
        fclose($in);
        fclose($out);
    }
    
    //save_image('http://www.someimagesite.com/img.jpg','image.jpg');
    
}